package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.Employee;
import com.itheima.reggie.service.EmployeeService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * @author Lff
 * @date 2022/2/25
 */
@Slf4j
@RestController()
@RequestMapping("/employee")
public class EmployeeController {
    @Autowired
    private EmployeeService employeeService;

    /**
     * 后台系统用户登录
     *
     * @param employee
     * @param req
     * @return
     */
    @PostMapping("/login")
    public R<Employee> login(@RequestBody Employee employee, HttpServletRequest req) {
        if (StringUtils.isEmpty(employee.getUsername())) {
            return R.error("请输入用户名！");
        }
        Employee login = employeeService.login(employee);
        String password = DigestUtils.md5DigestAsHex(employee.getPassword().getBytes());
        if (login == null) {
            return R.error("用户名错误!");
        }
        if (!login.getPassword().equals(password)) {
            return R.error("密码错误！");
        }
        if (login.getStatus() == 0) {
            return R.error("账号被禁用，请联系管理员处理。");
        }
        log.info("正在登陆，用户id为{}", login.getId());
        req.getSession().setAttribute("emp", login.getId());
        login.setPassword(null);
        return R.success(login);
    }

    /**
     * 后台系统用户退出登录
     *
     * @param req
     * @return
     */
    @PostMapping("/logout")
    public R<String> logout(HttpServletRequest req) {
        req.getSession().removeAttribute("emp");
        return R.success("退出成功！");
    }


    /**
     * 前台请求数据
     * 数据分页返回
     *
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @GetMapping("/page")
    public R<Page> page(Integer page, Integer pageSize, String name) {
        Page<Employee> pages = employeeService.getAllByPage(page, pageSize, name);
        return R.success(pages);
    }

    /**
     * 添加用户
     *
     * @param employee
     * @return
     */
    @PostMapping
    public R<String> save(@RequestBody Employee employee) {
        boolean flag = employeeService.addEmp(employee);
        if (!flag) {
            return R.error("添加失败！");
        }
        return R.success("添加员工信息成功！");
    }

    /**
     * 修改用户
     *
     * @param employee
     * @return
     */
    @PutMapping
    public R<String> update(@RequestBody Employee employee) {
        boolean flag = employeeService.updateEmp(employee);
        if (!flag) {
            return R.error("修改失败！");
        }
        return R.success("修改员工信息成功！");
    }


    /**
     * 用于用户修改数据回显
     */
    @GetMapping("/{id}")
    public R<Employee> getOneById(@PathVariable Long id) {
        Employee employee = employeeService.getById(id);
        return R.success(employee);
    }

    @GetMapping("/test")
    public void test() {
        System.out.println("test running...");
    }
}
